package org.paradise.dms.web.action.studentreport;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;


import net.sf.json.JSONArray;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.struts2.ServletActionContext;
import org.directwebremoting.annotations.RemoteProxy;
import org.paradise.dms.pojo.Student;
import org.paradise.dms.services.StudentReportService;
import org.paradise.dms.web.action.DMSBaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.opensymphony.xwork2.ActionContext;

/**
 * 
 * Description:学生登录Action
 * 
 * Copyright (c) paraDise sTudio(DT). All Rights Reserved.
 * 
 * @version 1.0 Mar 4, 2009 10:59:16 PM 李双江（paradise.lsj@gmail.com）created
 */
@Service
@RemoteProxy(name = "studentReportAction")
@Scope("prototype")
public class StudentReportAction extends DMSBaseAction {

	/**
	 * 
	 */
	@Autowired
	StudentReportService studentReportService;
	private String studentno0;
	private String password0;
	private String studentno1;
	private String password1;
	private String studentno2;
	private String password2;
	private String studentno3;
	private String password3;
	private String studentno4;
	private String password4;

	
	
	//进入单人申请的页面
	public String enterSingleApp(){
		return SUCCESS;
	}
	//多人申请时ajax校验
	public String validateStudent(String studentno,String password ){
		return studentReportService.validateStudent(studentno,password);
		
	}
	//多人申请时struts校验
	public Student validateStudentExits(String studentno,String password ){
		//System.out.println(password+"---------------"+studentno);
		return studentReportService.validateStudentExits(studentno,password);
		
	}
	//集体申请进入页面
	public String enterDormShowAndChoose(){
		List<Student> sl = new ArrayList<Student>();
		
		if("".equals(studentno0.trim()) && "".equals(password0.trim())
			&& "".equals(studentno1.trim()) && "".equals(password1.trim())
			&& "".equals(studentno2.trim()) && "".equals(password2.trim())
			&& "".equals(studentno3.trim()) && "".equals(password3.trim())
			&& "".equals(studentno4.trim()) && "".equals(password4.trim())){
			addFieldError("studentno0","若单人申请,请点击单人申请链接");
			return INPUT;
		}
		
		if(!StudentnoAndPasswordIsEmptyOrNotEmptyBoth(studentno0,password0) 
				|| !StudentnoAndPasswordIsEmptyOrNotEmptyBoth(studentno1,password1)
				|| !StudentnoAndPasswordIsEmptyOrNotEmptyBoth(studentno2,password2)
				|| !StudentnoAndPasswordIsEmptyOrNotEmptyBoth(studentno3,password3)
				|| !StudentnoAndPasswordIsEmptyOrNotEmptyBoth(studentno4,password4)){
			addFieldError("studentno0","每一对用户名和密码请不要只填其中之一,如果未设密码,该用户可能还未激活,请先激活");
			return INPUT;
		}
		if(checkStudentnoAndPassword(studentno0,password0)){
			Student s = validateStudentExits(studentno0,password0);
			if(null == s){
				addFieldError("studentno0","账号或密码错误");
				return INPUT;
			}else sl.add(s);
		}
		if(checkStudentnoAndPassword(studentno1,password1)){
			Student s = validateStudentExits(studentno1,password1);
			if(null == s){
				addFieldError("studentno1","账号或密码错误");
				return INPUT;
			}else sl.add(s);
		}
		if(checkStudentnoAndPassword(studentno2,password2)){
			Student s = validateStudentExits(studentno2,password2);
			if(null == s){
				addFieldError("studentno2","账号或密码错误");
				return INPUT;
			}else sl.add(s);
		}
		if(checkStudentnoAndPassword(studentno3,password3)){
			Student s = validateStudentExits(studentno3,password3);
			if(null == s){
				addFieldError("studentno3","账号或密码错误");
				return INPUT;
			}else sl.add(s);
		}
		if(checkStudentnoAndPassword(studentno4,password4)){
			Student s = validateStudentExits(studentno4,password4);
			if(null == s){
				addFieldError("studentno4","账号或密码错误");
				return INPUT;
			}else sl.add(s);
		}
		
		//获取登录学生自己的信息
		HttpSession session = this.getRequest().getSession();
		String stuno = (String)session.getAttribute("studentno");
		Student myself = this.getReportSerivce().getStudentByNO(stuno);
		
		//自己已经被分配了宿舍,不可再分
		List list = this.studentReportService.findDormByStudentno(stuno);
		if(null != list && list.size()>0){
			addFieldError("studentno0","您已分配宿舍,不可操作集体申请");
			return INPUT;
		}
		for(Student s : sl){
			//表单人本应填写其他人的学号密码,不允许填写自己的
			if(s.getStudentno().equals(myself.getStudentno())){
				addFieldError("studentno0","请不要再填写自己的学号密码");
				return INPUT;
			}
			//只有具有相同的preasignid,才可以集体分配
			if(s.getStudentpreasignedid() != myself.getStudentpreasignedid()){
				addFieldError("studentno0",s.getStudentno()+"的宿舍标记与您不同,不可与您参与集体申请");
				return INPUT;
			}
			//集体申请中不可以有人已经被分配了宿舍
			List l = this.studentReportService.findDormByStudentno(s.getStudentno());
			if(null != l && l.size()>0){
				addFieldError("studentno0",s.getStudentno()+"已分配宿舍,不可与您参与集体申请");
				return INPUT;
			}
		}
		
		return SUCCESS;
	}
	//当用户名和密码都填了
	public Boolean checkStudentnoAndPassword(String sno,String pwd){
		if(!"".equals(sno.trim()) && !"".equals(pwd.trim())){
			return true;
		}else return false;
	}
	//用户名和密码要么都填 ，要么都不填
	public Boolean StudentnoAndPasswordIsEmptyOrNotEmptyBoth(String sno,String pwd){
		if(("".equals(sno.trim()) && "".equals(pwd.trim())) || (!"".equals(sno.trim()) && !"".equals(pwd.trim()))){
			return true;
		}else return false;
	}
	//根据sql查询所有可申请宿舍
	public JSONArray getAvailDormByPreAsignedid(String sql) {
		List sl = this.studentReportService.getAvailDormByPreAsignedid(sql);
		if (sl != null & sl.size() > 0) {
			JSONArray jsonArray = JSONArray.fromObject(sl);
			return jsonArray;
		}
		return null;
	}
	//根据sql查询所有可申请宿舍的数量,暂时不用
	public int getAvailDormCountByPreAsignedid(String sql) {
		return this.studentReportService.getAvailDormCountByPreAsignedid(sql);
	}
	
	//单人申请分配宿舍
	public String singleApply(String studentNO){
		return (String)this.studentReportService.insertSingleApply(studentNO);
	}
	
	//集体申请分配宿舍
	public String multiApply(String stunos){
		String [] stunosArray = stunos.split(",");
		return studentReportService.insertMultipleApply(stunosArray);
	}
	
	//分配宿舍后显示宿舍信息
	public String [] findDormByStudentno(String stuno){
		List l = this.studentReportService.findDormByStudentno(stuno);
		if(null != l && l.size()>0){
			//obj[0] : dormitoryid
			//obj[1] : dormitorydisplayname
			Object [] obj = (Object [])l.get(0);
			
			String [] result = new String[2];
			for(int a = 0;a<obj.length;a++){
				result[a] = obj[a].toString();
			}
			return result;
		}else return null;
		
	}
	//根据是否已经分配的宿舍，进入申请页面或退宿页面
	public  String applyJudge(){
		List l = this.studentReportService.findDormByStudentno(this.getRequest().getSession().getAttribute("studentno").toString());
		if(null != l && l.size()>0){
			Object [] obj = (Object [])l.get(0);
			this.getRequest().getSession().setAttribute("studentDormdisplayname", obj[1].toString());
			return INPUT;
		}else return SUCCESS;
		
	}
	//退宿
	public String dormCheckout(String studentno){
		return this.studentReportService.cancelAllocate(studentno);
	}
	
	//从新生报道退宿记录表查找退宿记录
	public String findDormCheckOut(String studentno){
		List l = this.studentReportService.findDormCheckOut(studentno);
		return l.size()+"";
	}
	
	
	public StudentReportService getReportSerivce() {
		return studentReportService;
	}
	public void setReportSerivce(StudentReportService reportSerivce) {
		this.studentReportService = reportSerivce;
	}
	public String getStudentno1() {
		return studentno1;
	}
	public void setStudentno1(String studentno1) {
		this.studentno1 = studentno1;
	}
	public String getPassword1() {
		return password1;
	}
	public void setPassword1(String password1) {
		this.password1 = password1;
	}
	public String getStudentno2() {
		return studentno2;
	}
	public void setStudentno2(String studentno2) {
		this.studentno2 = studentno2;
	}
	public String getPassword2() {
		return password2;
	}
	public void setPassword2(String password2) {
		this.password2 = password2;
	}
	public String getStudentno3() {
		return studentno3;
	}
	public void setStudentno3(String studentno3) {
		this.studentno3 = studentno3;
	}
	public String getPassword3() {
		return password3;
	}
	public void setPassword3(String password3) {
		this.password3 = password3;
	}
	public String getStudentno4() {
		return studentno4;
	}
	public void setStudentno4(String studentno4) {
		this.studentno4 = studentno4;
	}
	public String getPassword4() {
		return password4;
	}
	public void setPassword4(String password4) {
		this.password4 = password4;
	}
	public String getStudentno0() {
		return studentno0;
	}
	public void setStudentno0(String studentno0) {
		this.studentno0 = studentno0;
	}
	public String getPassword0() {
		return password0;
	}
	public void setPassword0(String password0) {
		this.password0 = password0;
	}

	
	
}
